
Imports std

Namespace language
    Namespace japanese

        Public Module ___kata2pron___

            Public ReadOnly repeat() As Char = {hextochar("0x30fd"), hextochar("0x30fe")}
            Public ReadOnly longpron() As Char = {hextochar("0x30fc")}

            Public ReadOnly pron(,) As String = { _
                            {hextochar("0x30a1"), shortenformat("la")}, _
                            {hextochar("0x30a2"), shortenformat("a")}, _
                            {hextochar("0x30a3"), shortenformat("li")}, _
                            {hextochar("0x30a4"), shortenformat("i")}, _
                            {hextochar("0x30a5"), shortenformat("lu")}, _
                            {hextochar("0x30a6"), shortenformat("u")}, _
                            {hextochar("0x30a7"), shortenformat("le")}, _
                            {hextochar("0x30a8"), shortenformat("e")}, _
                            {hextochar("0x30a9"), shortenformat("lo")}, _
                            {hextochar("0x30aa"), shortenformat("o")}, _
                            {hextochar("0x30ab"), shortenformat("ka")}, _
                            {hextochar("0x30ac"), shortenformat("ga")}, _
                            {hextochar("0x30ad"), shortenformat("ki")}, _
                            {hextochar("0x30ae"), shortenformat("gi")}, _
                            {hextochar("0x30af"), shortenformat("ku")}, _
                            {hextochar("0x30b0"), shortenformat("gu")}, _
                            {hextochar("0x30b1"), shortenformat("ke")}, _
                            {hextochar("0x30b2"), shortenformat("ge")}, _
                            {hextochar("0x30b3"), shortenformat("ko")}, _
                            {hextochar("0x30b4"), shortenformat("go")}, _
                            {hextochar("0x30b5"), shortenformat("sa")}, _
                            {hextochar("0x30b6"), shortenformat("za")}, _
                            {hextochar("0x30b7"), shortenformat("si")}, _
                            {hextochar("0x30b8"), shortenformat("zi")}, _
                            {hextochar("0x30b9"), shortenformat("su")}, _
                            {hextochar("0x30ba"), shortenformat("zu")}, _
                            {hextochar("0x30bb"), shortenformat("se")}, _
                            {hextochar("0x30bc"), shortenformat("ze")}, _
                            {hextochar("0x30bd"), shortenformat("so")}, _
                            {hextochar("0x30be"), shortenformat("zo")}, _
                            {hextochar("0x30bf"), shortenformat("ta")}, _
                            {hextochar("0x30c0"), shortenformat("da")}, _
                            {hextochar("0x30c1"), shortenformat("ti")}, _
                            {hextochar("0x30c2"), shortenformat("di")}, _
                            {hextochar("0x30c3"), shortenformat("ltu")}, _
                            {hextochar("0x30c4"), shortenformat("tu")}, _
                            {hextochar("0x30c5"), shortenformat("du")}, _
                            {hextochar("0x30c6"), shortenformat("te")}, _
                            {hextochar("0x30c7"), shortenformat("de")}, _
                            {hextochar("0x30c8"), shortenformat("to")}, _
                            {hextochar("0x30c9"), shortenformat("do")}, _
                            {hextochar("0x30ca"), shortenformat("na")}, _
                            {hextochar("0x30cb"), shortenformat("ni")}, _
                            {hextochar("0x30cc"), shortenformat("nu")}, _
                            {hextochar("0x30cd"), shortenformat("ne")}, _
                            {hextochar("0x30ce"), shortenformat("no")}, _
                            {hextochar("0x30cf"), shortenformat("ha")}, _
                            {hextochar("0x30d0"), shortenformat("ba")}, _
                            {hextochar("0x30d1"), shortenformat("pa")}, _
                            {hextochar("0x30d2"), shortenformat("hi")}, _
                            {hextochar("0x30d3"), shortenformat("bi")}, _
                            {hextochar("0x30d4"), shortenformat("pi")}, _
                            {hextochar("0x30d5"), shortenformat("hu")}, _
                            {hextochar("0x30d6"), shortenformat("bu")}, _
                            {hextochar("0x30d7"), shortenformat("pu")}, _
                            {hextochar("0x30d8"), shortenformat("he")}, _
                            {hextochar("0x30d9"), shortenformat("be")}, _
                            {hextochar("0x30da"), shortenformat("pe")}, _
                            {hextochar("0x30db"), shortenformat("ho")}, _
                            {hextochar("0x30dc"), shortenformat("bo")}, _
                            {hextochar("0x30dd"), shortenformat("po")}, _
                            {hextochar("0x30de"), shortenformat("ma")}, _
                            {hextochar("0x30df"), shortenformat("mi")}, _
                            {hextochar("0x30e0"), shortenformat("mu")}, _
                            {hextochar("0x30e1"), shortenformat("me")}, _
                            {hextochar("0x30e2"), shortenformat("mo")}, _
                            {hextochar("0x30e3"), shortenformat("lya")}, _
                            {hextochar("0x30e4"), shortenformat("ya")}, _
                            {hextochar("0x30e5"), shortenformat("lyu")}, _
                            {hextochar("0x30e6"), shortenformat("yu")}, _
                            {hextochar("0x30e7"), shortenformat("lyo")}, _
                            {hextochar("0x30e8"), shortenformat("yo")}, _
                            {hextochar("0x30e9"), shortenformat("ra")}, _
                            {hextochar("0x30ea"), shortenformat("ri")}, _
                            {hextochar("0x30eb"), shortenformat("ru")}, _
                            {hextochar("0x30ec"), shortenformat("re")}, _
                            {hextochar("0x30ed"), shortenformat("ro")}, _
                            {hextochar("0x30ee"), shortenformat("lwa")}, _
                            {hextochar("0x30ef"), shortenformat("wa")}, _
                            {hextochar("0x30f0"), shortenformat("wi")}, _
                            {hextochar("0x30f1"), shortenformat("we")}, _
                            {hextochar("0x30f2"), shortenformat("o")}, _
                            {hextochar("0x30f3"), shortenformat("nn")}, _
                            {hextochar("0x30f4"), shortenformat("vu")}, _
                            {hextochar("0x30f5"), shortenformat("lka")}, _
                            {hextochar("0x30f6"), shortenformat("lke")}}

            Public Sub prepare(ByRef input As String)
                Dim index As Int32
                Dim i As Int32
                Dim len As Int32 = strlen(input)
                dbc2sbc(input)

                For i = 0 To repeat.Length() - 1
                    index = input.IndexOf(repeat(i))
                    While index < len And index > 0
                        strrplc(input, index, input(index - 1))
                        index = input.IndexOf(repeat(i), index + 1)
                    End While
                Next
            End Sub

            Public Function kata2pron(ByRef input As String, Optional ByVal shortenlevel As Int32 = 2) As String
                Dim i As Int32
                Dim word As String = Nothing

                If shortenlevel > 2 Then
                    shortenlevel = 2
                ElseIf shortenlevel < 0 Then
                    shortenlevel = 0
                End If
                copy(word, input)
                prepare(word)
                For i = 0 To pron.GetLength(0) - 1
                    word = word.Replace(pron(i, 0), pron(i, 1))
                Next

                If shortenlevel > 0 Then
                    pronshorten(word, 1)
                End If
                If shortenlevel > 1 Then
                    pronshorten(word, 2)
                End If
                cleanseperator(word)
                fixlongpron(word)
                Return word
            End Function

        End Module
    End Namespace
End Namespace
